package org.qiuqiu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.qiuqiu.service.ClickService;
import org.qiuqiu.service.impl.ClickServiceImpl;
import org.qiuqiu.service.impl.PostServiceImpl;
import org.qiuqiu.service.impl.ReplyServiceImpl;
import org.qiuqiu.utils.DateUtil;
import org.qiuqiu.vo.Click;
import org.qiuqiu.vo.ClickExample;
import org.qiuqiu.vo.PaginationVO;
import org.qiuqiu.vo.Post;
import org.qiuqiu.vo.Reply;
import org.qiuqiu.vo.ReplyExample;
import org.qiuqiu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping(value = "/reply")
public class ReplyController {
	@Autowired
	private ReplyServiceImpl service;

	@Autowired
	private PostServiceImpl postService;
	@Autowired
	private ClickServiceImpl clickService;
    private Logger log=Logger.getLogger(this.getClass());
	// 得到所有回贴
	@RequestMapping(value = "/replies/{id}")
	public String getAllReply(Model model, @PathVariable("id") int postId,
			PaginationVO page, HttpSession session) throws Exception {
		List<Reply> list = service.findallReplyBypostId(postId, page);
		int total = service.totalBypostId(postId);
		page.setTotalField(total);
		System.out.println("+++++++++++++++++" + list);
		Post post = postService.selectByPrimaryKey(postId);
		// 向点击表里面插数据
		User user = (User) session.getAttribute("user");
		if (user != null) {
			ClickExample example = new ClickExample();
			example.createCriteria().andPostidEqualTo(postId)
					.andUseridEqualTo(user.getUserid());
			List<Click> clicks = clickService.selectByExample(example);
			System.out.println("s12323"+clicks);
			if (clicks.isEmpty() ) {
				Click c = new Click();
				c.setPostid(postId);
				c.setUserid(user.getUserid());
				clickService.insertSelective(c);
			}
		}
		model.addAttribute("post", post);
		model.addAttribute("replies", list);
		model.addAttribute("page", page);
		return "content";
	}

	// 发布新回复
	@RequestMapping(value = "/newreply")
	public  void newReply(Model model,HttpServletResponse rs,
			@RequestParam("replyContent") String reply_content,
			/*@RequestParam("userid") int userId,
			@RequestParam("nickname") String nickName,*/
			@RequestParam("postid") int postId, HttpSession session)
			 {
		String flag="no";
		Gson gson = new GsonBuilder().create();
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out=null;
		try {
			 out = rs.getWriter();
				User user = (User) session.getAttribute("user");
				if (user == null) {
					model.addAttribute("msg", "请先登录！");
					flag="login";
				}else{
				Reply reply = new Reply();
				reply.setReplyUsername(user.getNickname());
				reply.setReplyContent(reply_content);
				reply.setReplyCreatetime(new Date());
				postService.updateRe(postId);
				reply.setPostid(postId);
				reply.setUserid(user.getUserid());
				service.insertSelective(reply);
				flag="ok";
				}
				
		} catch (IOException e) {
			flag="no";
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.write(gson.toJson(flag));
			out.flush();
			out.close();
		}
		
		
	}
}
